tg-me.com/pythondaily/32767
Last Update:
Storing discount info in purchase item as a JSON field or using a separate model?
During the pass few days I've been debating how to best store the discount information in my purchase items.
We allow the users of our marketplace to modify the "Discount" model, for example if they want to change the amount of discount for the same code over time, etc.
Because of this we want to store the actual values that the discount code had at the time of the purchase.
**Option A:** Using a JSON field. In this case we can just create a dictionary with the most important info and store it inside a single field.
class PurchaseItem(models.Model):
...
discount = models.JSONField(null=True, blank=True)
And the discount dictionary would look like this:
{"id": 5, "code": "HELLO10", "type": "vendor", "amount": "32.00", "value": "28.80", "is_fixed": false}
**Option B:** Second option is to create a separate model to store this information, instead of JSON.
class PurchaseItem(models.Model):
...
discount = models.ForeignKey(DiscountApplied, null=True)
class DiscountApplied(BaseModel):
/r/django
https://redd.it/1cx1cmc
BY Python Daily
Share with your friend now:
tg-me.com/pythondaily/32767